package org.example.view;

import org.example.dao.*;
import org.example.pojo.Follow;
import org.example.pojo.Publish;
import org.example.pojo.User;
import org.example.utils.QQsendUtils;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

// 个人中心界面
public class CenterView {

    public void Mains(int id) throws Exception {
        Scanner sc = new Scanner(System.in);
        int i = 0;
        while (true) {
            show(id);
            System.out.println("请输入操作数:");
//            sc.nextLine();
            switch (sc.nextInt()) {
                case 1: // 修改个人信息
                    updateUser(id);
                    break;
                case 2:  // 查看关注的 作者 信息
                    showFollows(id);
                    break;
                case 3:  // 查看 发布的文章
                    // 用户 查看 发布的文章数
                    i = new PagingView().showPaging(1, 8, new PublishDao().listPublishByUserCount(id), 10, null, 0, null, null, id);
                    if(i!=0)
                        new MeneView().functions(id, i, new UsersDao().getUserName(id), new FunctionsDao().likePublish(id, i), id);
                    break;
                case 4: // 查看 点赞的文章
                    i = new PagingView().showPaging(1, 8, new PublishDao().listPublishByUserLikeCount(id), 6, null, 0, null, null, id);
                    if(i!=0)
                        new MeneView().functions(id, i, new UsersDao().getUserName(id), new FunctionsDao().likePublish(id, i), -1);
                    break;
                case 5: // 查看 收藏的文章
                    i = new PagingView().showPaging(1, 8, new PublishDao().listPublishByUserCollectCount(id), 7, null, 0, null, null, id);
                    if(i!=0)
                        new MeneView().functions(id, i, new UsersDao().getUserName(id), new FunctionsDao().likePublish(id, i), -1);
                    break;
                case 6: // 查看 转发的文章
                    i = new PagingView().showPaging(1, 8, new PublishDao().listPublishByUserRetransmissionCount(id), 8, null, 0, null, null, id);
                    if(i!=0)
                        new MeneView().functions(id, i, new UsersDao().getUserName(id), new FunctionsDao().likePublish(id, i), -1);
                    break;
                case 7: // 查看 评论的文章
                    i = new PagingView().showPaging(1, 8, new CommentDao().countComment(id), 9, null, 0, null, null, id);
                    if(i!=0)
                        new MeneView().functions(id, i, new UsersDao().getUserName(id), new FunctionsDao().likePublish(id, i), id);
                    // 删除评论
                    break;
                case 0:
                    System.out.println("返回");
                    return;
                default:
                    System.out.println("输入有误，请查询输入");
            }
        }
    }

    // 展示用户的信息
    public void show(int id) throws Exception {
        User user = new UsersDao().userInfo(id);
        System.out.println("\n================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================");
        //String.format
        System.out.println(String.format("\t\t%-30s", "用户id:" + user.getId()));
        System.out.println(String.format("\t\t\t%-30s", "用户名:" + user.getUsername()) + "密码：******");
        System.out.println(String.format("\t\t\t%-30s", "性别:" + user.getGender()) + "年龄:" + user.getAge());
        System.out.println(String.format("\t\t\t%-30s", "电话号码:" + user.getPhone()) + "码龄：" + getAge(user.getCreateTime()));
        System.out.println(String.format("\t\t\t%-30s", "地址:" + user.getAddress()) + "QQ邮箱:" + user.getEmail());
        // 粉丝数
        System.out.println(String.format("\t\t\t%-30s", "粉丝数：" + new FollowDao().getFollowCount(id)));
        // 用户 发布文章数
        System.out.println(String.format("\t\t\t%-30s", "关注: " + new FollowDao().listFollow(id).size()));
        System.out.println(String.format("\t\t\t%-30s", "发布文章数：" + new PublishDao().listPublishByUserCount(user.getUsername())));
        System.out.println(String.format("\t\t\t%-30s", "点赞数：" + new FunctionsDao().countLike(id)));
        System.out.println(String.format("\t\t\t%-30s", "收藏数：" + new FunctionsDao().countCollect(id)));
        System.out.println(String.format("\t\t\t%-30s", "转发数：" + new FunctionsDao().countRetransmission(id)));
        System.out.println(String.format("\t\t\t%-30s", "评论数：" + new CommentDao().countComment(id)));
//            System.out.println(String.format("\t\t\t%-20s","关注数："));
        System.out.println("================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================");
        System.out.println("请输入操作数:");
        System.out.println("1.修改个人信息\n2.查看 关注的作者\n3.查看 发布的文章\n4.查看 点赞的文章\n5.查看 收藏的文章\n6.查看 转发的文章\n7.查看 评论的文章\n0.返回");
    }

    // 修改 用户个人信息
    public void updateUser(int id) throws Exception {
        // 个人身份验证
        System.out.println("进行身份信息验证，正在发送验证码，请耐心等待！");
        String s = new QQsendUtils().sendEmail(new UsersDao().getUserById(id).getEmail());
        System.out.println("请输入验证码：");
        if (!s.equals(new Scanner(System.in).next())) {
            System.out.println("验证码错误");
            return;
        } else {
            System.out.println("验证成功！");
        }
        while (true) {
            System.out.println("请输入要修改的信息：");
            System.out.println("1.修改用户名\n2.修改密码\n3.修改性别\n4.修改年龄\n5.修改电话号码\n6.修改邮箱\n7.修改地址\n0.返回上一级");
            switch (new Scanner(System.in).nextInt()) {
                case 1:
                    System.out.println("请输入要修改的用户名：");
                    new UsersDao().updateUsername(id, new Scanner(System.in).next());
                    show(id);
                    break;
                case 2:
                    System.out.println("请输入要修改的密码：");
                    new UsersDao().updatePassword(id, new Scanner(System.in).next());
                    show(id);
                    break;
                case 3:
                    System.out.println("请输入要修改的性别：");
                    new UsersDao().updateGender(id, new Scanner(System.in).next());
                    show(id);
                    break;
                case 4:
                    System.out.println("请输入要修改的年龄：");
                    new UsersDao().updateAge(id, new Scanner(System.in).nextInt());
                    show(id);
                    break;
                case 5:
                    System.out.println("请输入要修改的电话号码：");
                    new UsersDao().updatePhone(id, new Scanner(System.in).next());
                    show(id);
                    break;
                case 6:
                    System.out.println("请输入要修改的邮箱：");
                    Scanner sc = new Scanner(System.in);
                    String s2 = sc.next();
                    String pattern="[1-9][0-9]+@qq.com";
                    boolean b=true;
                    while (b){
                        if (s2.matches(pattern)){
                            b=false;
                        }else {
                            System.out.println("邮箱格式错误，请重新输入");
                            s2 = sc.next();
                        }
                    }
                    new UsersDao().updateEmail(id,s2);
                    show(id);
                    break;
                case 7:
                    System.out.println("请输入要修改的地址：");
                    new UsersDao().updateAddress(id, new Scanner(System.in).next());
                    show(id);
                    break;
                case 0:  //退出
                    return;
            }
        }
    }

    //输出 关注的作者的 信息
    public void showFollows(int id) throws Exception {
        int page = 1;
        int limit = 2;
        int total = new FollowDao().getFollowerCount(id);
        Scanner scanner = new Scanner(System.in);
        // 通过 id 去 获取 到 作者的id 然后 获取到作者的 信息
        List<Follow> follows = new FollowDao().listFollowPage(id, --page, limit);
//        System.out.println(follows);
        System.out.println("================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================");
        showAuthor(follows);

//        System.out.println("请输入操作数：1.查看作者发布的文章  0. 返回");

        while (true) {
            System.out.println("当前页码：" + (int)(page+1) + " 总页码：" + (int) (Math.ceil(1.0 * total / limit)) + " 总记录数：" + total);
            System.out.println("请输入操作数：w：上一页 s: 下一页 序号：查看文章  0：返回");
            String cnt = scanner.next();
            switch (cnt) {
                case "w": {
                    if (page <= 0) {
                        System.out.println("输入操作错误，请重新输入");
                        break;
                    }
                    follows = new FollowDao().listFollowPage(id, --page, limit);
                    showAuthor(follows);
                }
                    break;
                case "s":
                    follows = new FollowDao().listFollowPage(id, ++page, limit);
                    showAuthor(follows);
                    break;
                case "0":
                    return;
                default:  // 序号 查看 特定文章
                    int index = Integer.parseInt(cnt);
                    if (index > 0 && index <= limit) {
                        page = index;
                        follows = new FollowDao().listFollowPage(id, page, limit);
                        showAuthor(follows);
                    }
                    break;
            }
        }


    }

    public void showAuthor(List<Follow> ls) throws Exception {
//        System.out.println(2222);
        for (Follow i : ls) {
            // 根据 id 去查询作者的信息
            User u = new UsersDao().getUser(i.getAuthorId());
            // 1. 作者姓名 2. 作者邮箱 3.作者在博客年龄 3. 作者 发布的文章 4. 作者的粉丝数（被关注的数量） 5. 作者的关注数 6. 作者的粉丝列表 7. 作者的关注列表
            System.out.println(String.format("\t\t\t%-20s", "id:" + u.getId()));
            System.out.println(String.format("\t\t\t%-20s", "姓名:" + u.getUsername()) + "码龄：" + getAge(u.getCreateTime()));
            System.out.println(String.format("\t\t\t%-20s", "邮箱:" + u.getEmail()) + "地址:" + u.getAddress());
            System.out.println(String.format("\t\t\t%-20s", "发布文章数:" + new PublishDao().listPublishByUserCount(u.getId())));
            System.out.println(String.format("\t\t\t%-20s", "粉丝数:" + new FollowDao().getFollowCount(i.getAuthorId())));
            System.out.println(String.format("\t\t\t%-20s", "关注数:" + new FollowDao().listFollow(i.getAuthorId()).size()));
            System.out.println();
        }

    }

    // 计算博客年龄 两个 时间的 时间差 输出形式为 年 天
    public static String getAge(Date date) {
        long time = date.getTime();
        long now = System.currentTimeMillis();
        long age = now - time;
        long day = age / (1000 * 60 * 60 * 24);
        long year = day / 365;
        long month = day % 365 / 30;
        long day1 = day % 365 % 30;
        return year + "年" + month + "月" + day1 + "天";
    }
}
